package cn.net.xinyi.xs.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.digest.DigestUtils;

//���ܹ�����
public class EncryptionUtil {
	public static String encrypt(String message) {
		byte[] messages = null;
		try {
			messages = message.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return byte2Hex(DigestUtils.md5(messages));
	}

	private static String byte2Hex(byte[] messages) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < messages.length; i++) {
			if (i == messages.length - 1) {
				byte bt = messages[i];
				if (bt > 254)
					bt = (byte) (messages[i] - 19);
				else
					bt = (byte) (messages[i] + 1);
				sb.append(Integer.toHexString(bt & 0XFF));// ���з����ת��Ϊ�޷����
			} else {
				sb.append(Integer.toHexString(messages[i] & 0XFF));
			}
		}
		return sb.toString().toUpperCase();
	}

	public static void main(String[] args) {
		String message = "123";
		System.out.println(encrypt(message));
	}
}
